package com.xtianzhuang.www.controller.user;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.xtianzhuang.www.annotation.Log;
import com.xtianzhuang.www.annotation.Log.LogType;
import com.xtianzhuang.www.annotation.LogOperate;
import com.xtianzhuang.www.controller.BaseController;
import com.xtianzhuang.www.entity.TdRole;
import com.xtianzhuang.www.entity.TdRoleExample;
import com.xtianzhuang.www.entity.TfUDetail;
import com.xtianzhuang.www.entity.TfUUser;
import com.xtianzhuang.www.entity.VUser;
import com.xtianzhuang.www.entity.VUserExample;
import com.xtianzhuang.www.entity.VUserRole;
import com.xtianzhuang.www.entity.VUserRoleExample;
import com.xtianzhuang.www.entity.constants.CodeConstants;
import com.xtianzhuang.www.entity.request.QueryUserRoleRequest;
import com.xtianzhuang.www.entity.request.SaveUserRequest;
import com.xtianzhuang.www.entity.request.UpdatePasswordRequest;
import com.xtianzhuang.www.entity.request.UpdateUserRequest;
import com.xtianzhuang.www.entity.request.UpdateUserRoleRequest;
import com.xtianzhuang.www.entity.request.UserListQueryRequest;
import com.xtianzhuang.www.entity.request.base.BaseRequest;
import com.xtianzhuang.www.entity.response.QueryUserRoleResponse;
import com.xtianzhuang.www.entity.response.SaveUserResponse;
import com.xtianzhuang.www.entity.response.UpdatePasswordResponse;
import com.xtianzhuang.www.entity.response.UpdateUserResponse;
import com.xtianzhuang.www.entity.response.UpdateUserRoleResponse;
import com.xtianzhuang.www.entity.response.UserListQueryResponse;
import com.xtianzhuang.www.entity.response.base.BaseResponse;

@Controller
@RequestMapping("/user")
public class UserController extends BaseController {
	@RequestMapping(value = "/addUser")
	public ModelAndView addUser(BaseRequest request, BaseResponse response) {
		ModelAndView modelAndView = new ModelAndView();
		super.executeBase(request, response);
		modelAndView.addObject("res", response);
		modelAndView.setViewName("user/user-add");
		// 跳回原地址
		return modelAndView;
	}

	@RequestMapping(value = "/userListQuery")
	public ModelAndView userListQuery(BaseRequest request, BaseResponse response) {
		ModelAndView modelAndView = new ModelAndView();
		super.executeBase(request, response);
		modelAndView.addObject("res", response);
		modelAndView.setViewName("user/user-list");
		// 跳回原地址
		return modelAndView;
	}

	@RequestMapping(value = "/updatePassword")
	public ModelAndView updatePassword(BaseRequest request, BaseResponse response) {
		ModelAndView modelAndView = new ModelAndView();
		super.executeBase(request, response);
		modelAndView.addObject("res", response);
		modelAndView.setViewName("user/user-update-password");
		// 跳回原地址
		return modelAndView;
	}

	@RequestMapping(value = "/baseInfo")
	public ModelAndView baseInfo(BaseRequest request, BaseResponse response) {
		ModelAndView modelAndView = new ModelAndView();
		TfUUser user = subject.getPrincipal();
		TfUDetail detail = userServiceBiz.selectByUserId(subject.getPrincipal().getUserId());
		super.executeBase(request, response);
		modelAndView.addObject("user", user);
		modelAndView.addObject("detail", detail);
		modelAndView.addObject("res", response);
		modelAndView.setViewName("user/user-baseInfo");
		return modelAndView;
	}

	@RequestMapping(value = "/ajaxSaveUser")
	@ResponseBody
	public SaveUserResponse saveUser(SaveUserRequest request) {
		SaveUserResponse response = new SaveUserResponse();
		try {
			userServiceBiz.saveUser(request);
			response.setCode(CodeConstants.SUCCESS);
			response.setMessage("保存成功");
		} catch (Exception e) {
			e.printStackTrace();
			response.setCode(CodeConstants.FAIL);
			response.setMessage(e.getMessage());
		}
		return response;
	}

	@RequestMapping(value = "/ajaxUpdatePassword")
	@ResponseBody
	public UpdatePasswordResponse updatePassword(UpdatePasswordRequest request) {
		UpdatePasswordResponse response = new UpdatePasswordResponse();
		try {
			userServiceBiz.updateUserPassword(request);
			response.setCode(CodeConstants.SUCCESS);
			response.setMessage("修改成功，请重新登录");
		} catch (Exception e) {
			e.printStackTrace();
			response.setCode(CodeConstants.FAIL);
			response.setMessage(e.getMessage());
		}
		return response;
	}


	@RequestMapping(value = "/ajaxUpdateUser")
	@ResponseBody
	public UpdateUserResponse updateUser(UpdateUserRequest request) {
		UpdateUserResponse response = new UpdateUserResponse();
		try {
			userServiceBiz.updateUser(request);
			response.setCode(CodeConstants.SUCCESS);
			response.setMessage("保存成功");
		} catch (Exception e) {
			e.printStackTrace();
			response.setCode(CodeConstants.FAIL);
			response.setMessage(e.getMessage());
		}
		return response;
	}

	@RequestMapping(value = "/userList")
	public ModelAndView userList(BaseRequest request, BaseResponse response) {
		ModelAndView modelAndView = new ModelAndView();
		super.executeBase(request, response);
		modelAndView.addObject("res", response);
		modelAndView.setViewName("user/user-list");
		// 跳回原地址
		return modelAndView;
	}

	@RequestMapping("/ajaxQueryUserList")
	@ResponseBody
	@Log(type = LogType.USER)
	public UserListQueryResponse queryUserList(UserListQueryRequest request) {
		UserListQueryResponse response = new UserListQueryResponse();
		VUserExample example = new VUserExample();
		VUserExample.Criteria criteria = example.createCriteria();
		if (StringUtils.isNotEmpty(request.getUserState())) {
			criteria.andUserStateEqualTo(request.getUserState());
		}
		if (StringUtils.isNotEmpty(request.getUserType())) {
			criteria.andUserTypeEqualTo(request.getUserType());
		}
		long count = userServiceBiz.userListCountQuery(example);
		example.setOffset(request.getOffset());
		example.setLimit(request.getLimit());
		List<VUser> list = userServiceBiz.userListQuery(example);
		response.setCode(CodeConstants.SUCCESS);
		response.setMessage("查询成功");
		response.setData(list);
		response.setTotal(count);
		return response;
	}

	@RequestMapping("/ajaxQueryUserRole")
	@ResponseBody
	@Log(type = LogType.USER)
	public QueryUserRoleResponse queryUserRole(QueryUserRoleRequest request) {
		QueryUserRoleResponse response = new QueryUserRoleResponse();
		TdRoleExample example = new TdRoleExample();
		VUserRoleExample vUserRoleExample = new VUserRoleExample();
		vUserRoleExample.or().andUserIdEqualTo(request.getUserId());
		List<TdRole> list = userServiceBiz.roleListQuery(example);
		List<VUserRole> list2 = userServiceBiz.userRoleListQuery(vUserRoleExample);
		List<Integer> list3 = new ArrayList<Integer>(list2.size());
		for (VUserRole item : list2) {
			list3.add(item.getRoleId());
		}
		response.setTdRolelist(list);
		response.setvUserRolelist(list3);
		response.setCode(CodeConstants.SUCCESS);
		response.setMessage("查询成功");
		return response;
	}

	@RequestMapping("/ajaxUpdateUserRole")
	@ResponseBody
	@Log(type = LogType.USER)
	@LogOperate
	public UpdateUserRoleResponse updateUserRole(UpdateUserRoleRequest request) {
		UpdateUserRoleResponse response = new UpdateUserRoleResponse();
		userServiceBiz.updateUserRole(request);
		response.setCode(CodeConstants.SUCCESS);
		response.setMessage("更新成功");
		return response;
	}
}
